# Import the used modules
import cv2
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline
image = cv2.imread('zootopia.jpg')
get the width and height of the image
def image_height_width(image):
""" This function takes an image and return its height and width """
h, w = image.shape[:2]
return (h,w)
Display image
def display_image(image):
""" A simple function to display the image in jupyter notebook with an appropriate size """
img = image.copy()
display_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
image_height, image_width = image_height_width(img)
plt.figure(figsize=(image_width//100,image_height//100))
plt.imshow(display_image);
display_image(image)
def BGR_to_RGB_without_cv(image):
""" Simple way to convert image colors from BGR to RGB without using opencv """
B = image[:,:,0]
G = image[:,:,1]
R = image[:,:,2]
img = image.copy()
img[:,:,0] = R
img[:,:,1] = G
img[:,:,2] = B
plt.imshow(img);
BGR_to_RGB_without_cv(image)
def BGR_to_grayscale_without_opencv(image):
""" Simple way to convert image colors from BGR to grayscale without using opencv """
B = image[:,:,0]
G = image[:,:,1]
R = image[:,:,2]
img = image.copy()
avg = (0.3*R) + (0.59*G) + (0.11*B)
img[:,:,0] = avg
img[:,:,1] = avg
img[:,:,2] = avg
plt.imshow(img);
BGR_to_grayscale_without_opencv(image)
Note: OpenCV arranges the channels in BGR order by default. So the 0th value will correspond to Blue pixel and not Red.
def from_RGB_to_BGR(image):
""" This function takes the required image and convert it from RGB to BGR and return the converted matrix"""
img = image.copy()
from_RGB_to_BGR = cv2.cvtColor(img,cv2.COLOR_RGB2BGR)
return from_RGB_to_BGR
#print RGB_to_BGR matrix
print(from_RGB_to_BGR(image))
[[[116 121 98] [110 118 94] [104 114 89] ... [116 92 68] [111 86 64] [117 92 72]] [[112 117 94] [115 123 99] [112 122 97] ... [113 89 65] [123 98 76] [110 85 65]] [[117 122 99] [112 120 96] [111 121 96] ... [113 89 65] [109 84 62] [114 89 69]] ... [[ 2 4 0] [ 35 39 25] [ 14 17 8] ... [ 1 1 1] [ 1 1 1] [ 1 1 1]] [[ 19 22 11] [ 74 79 56] [ 21 27 1] ... [ 1 1 1] [ 1 1 1] [ 1 1 1]] [[ 18 21 10] [ 99 106 73] [ 61 69 32] ... [ 1 1 1] [ 1 1 1] [ 1 1 1]]]
display_image(from_RGB_to_BGR(image))
def from_RGB_to_grayscale(image):
""" This function takes the required image and convert it from RGB to grayscale and return the converted matrix"""
img = image.copy()
from_RGB_to_grayscale = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
return from_RGB_to_grayscale
# print RGB_to_grayscale matrix
print(from_RGB_to_grayscale(image))
[[114 110 105 ... 88 82 89] [110 115 113 ... 85 94 82] [115 112 112 ... 85 80 86] ... [ 3 34 14 ... 1 1 1] [ 18 72 19 ... 1 1 1] [ 17 95 57 ... 1 1 1]]
display_image(from_RGB_to_grayscale(image))
def from_BGR_to_grayscale(image):
""" This function takes the required image and convert it from BGR to grayscale and return the converted matrix"""
img = image.copy()
from_BGR_to_grayscale = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
return from_BGR_to_grayscale
#print BGR_to_grayscale matrix
print(from_BGR_to_grayscale(image))
[[117 113 108 ... 96 91 97] [113 118 116 ... 93 103 90] [118 115 115 ... 93 89 94] ... [ 3 36 15 ... 1 1 1] [ 20 75 22 ... 1 1 1] [ 19 100 62 ... 1 1 1]]
display_image(from_BGR_to_grayscale(image))
def image_normalization(image):
""" This function changes the range of pixel intensity values to increase contrast of image """
img = image.copy()
img_height, img_width = image_height_width(img)
norm_img = np.zeros((img_height,img_width))
final_img = cv2.normalize(img, norm_img, 0, 255, cv2.NORM_MINMAX)
display_image(final_img)
image_normalization(image)
def fixed_rotation(image,direction='right'):
""" This function rotate the image 90 degree clockwise each time """
img = image.copy()
#the default rotation
rotated_image = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
if direction == 'left':
rotated_image = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
elif direction == 'bottom':
rotated_image = cv2.rotate(img, cv2.ROTATE_180)
#display the rotated image
display_image(rotated_image)
#The default rotation to the right
fixed_rotation(image)
#rotate the image to the left
fixed_rotation(image, 'left')
#rotate the image to the bottom
fixed_rotation(image,'bottom')
def image_rotation(image, angle):
""" This function rotate the image by any angle """
img = image.copy()
image_height, image_width = image_height_width(img)
#get the center of the image
center = (image_width//2 , image_height//2)
#Generate the rotation matrix
matrix = cv2.getRotationMatrix2D(center, angle, 1.0)
#perform the affine transformation
rotated = cv2.warpAffine(img, matrix, (image_width,image_height))
#display the rotated image
display_image(rotated)
image_rotation(image, 40)
def horizontal_flipping(image):
""" This function flips the image around the y-axis """
img = image.copy()
#flipped_image = cv2.flip(image,1)
flipped_image = img[:,::-1]
display_image(flipped_image)
horizontal_flipping(image)
def vertical_flipping(image):
""" This function flips the image around the x-axis """
img = image.copy()
#flipped_image = cv2.flip(image,0)
flipped_image = img[::-1]
display_image(flipped_image)
vertical_flipping(image)
def right_half_crop(image):
""" this function crops the right half of the image """
img = image.copy()
image_width = image_height_width(img)[1]
right_crop = img[:, 0:image_width//2]
display_image(right_crop)
right_half_crop(image)
def left_half_crop(image):
""" this function crops the left half of the image """
img = image.copy()
image_width = image_height_width(img)[1]
left_crop = img[:, image_width//2:image_width]
display_image(left_crop)
left_half_crop(image)
def lower_half_crop(image):
""" This function crops the lower part of the image """
img = image.copy()
image_height = image_height_width(img)[0]
lower_crop = img[0:image_height//2 ,:]
display_image(lower_crop)
lower_half_crop(image)
def upper_half_crop(image):
""" This function crops the lower part of the image """
img = image.copy()
image_height = image_height_width(img)[0]
upper_crop = img[image_height//2:image_height ,:]
display_image(upper_crop)
upper_half_crop(image)
#get the height and width of the image
image_height, image_width = image_height_width(image)
#split measures of the grid
image_height_grid = image_height // 3
image_width_grid = image_width // 3
img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
#1 2 3
#4 5 6
#7 8 9
# First row
plt.figure(figsize = [20, 7])
#1
plt.subplot(1,3,1)
plt.imshow(img[0:image_height_grid , 0:image_width_grid])
#2
plt.subplot(1,3,2)
plt.imshow(img[0:image_height_grid , image_width_grid:image_width_grid*2])
#3
plt.subplot(1,3,3)
plt.imshow(img[0:image_height_grid , image_width_grid*2:image_width]);
#Second row
plt.figure(figsize = [20, 7])
#4
plt.subplot(1,3,1)
plt.imshow(img[image_height_grid:image_height_grid*2 , 0:image_width_grid])
#5
plt.subplot(1,3,2)
plt.imshow(img[image_height_grid:image_height_grid*2 , image_width_grid:image_width_grid*2])
#6
plt.subplot(1,3,3)
plt.imshow(img[image_height_grid:image_height_grid*2 , image_width_grid*2:image_width]);
#Third row
plt.figure(figsize = [20, 7])
#7
plt.subplot(1,3,1)
plt.imshow(img[image_height_grid*2:image_height , 0:image_width_grid])
#8
plt.subplot(1,3,2)
plt.imshow(img[image_height_grid*2:image_height , image_width_grid:image_width_grid*2])
#9
plt.subplot(1,3,3)
plt.imshow(img[image_height_grid*2:image_height , image_width_grid*2:image_width]);
def lower_right_corner_crop(image):
""" This function crop the image and get only the lower corner part """
img = image.copy()
#get the height and width of the image
image_height, image_width = image_height_width(img)
#split measures of the grid
image_height_grid = image_height // 3
image_width_grid = image_width // 3
#cv2.rectangle(original image, start point(width,height), end point(width,height), color, thicknessof rectangle)
cropped_image = cv2.rectangle(img, (2*(image_width_grid) , 2*(image_height_grid)), (image_width , image_height), (0,0,0), -1)
display_image(cropped_image)
lower_right_corner_crop(image)
def upper_left_corner_crop(image):
""" This function crop the image and get only the upper left corner part """
img = image.copy()
#get the height and width of the image
image_height, image_width = image_height_width(img)
#split measures of the grid
image_height_grid = image_height // 3
image_width_grid = image_width // 3
cropped_image = cv2.rectangle(img, (0,0), (image_width_grid,image_height_grid), (0,0,0), -1)
display_image(cropped_image)
upper_left_corner_crop(image)
def diagonal_crop(image):
""" This function crop the image diagonally """
img = image.copy()
#get the height and width of the image
image_height, image_width = image_height_width(img)
pts = np.array([[0,image_height] , [image_width,0] , [image_width,image_height]])
cv2.fillPoly(img, [pts], 0)
display_image(img)
diagonal_crop(image)
def negative_diagonal_crop(image):
""" This function crop the image diagonally (in negative direction) """
img = image.copy()
#get the height and width of the image
image_height, image_width = image_height_width(img)
pts = np.array([[0,0] , [0,image_height] , [image_width,image_height]])
cv2.fillPoly(img, [pts], 0)
display_image(img)
negative_diagonal_crop(image)
def center_crop(image):
""" This function crops the square in the center of the image """
img = image.copy()
height_image, width_image = image_height_width(img)
center = (height_image//2, width_image//2)
display_image(img[center[0]-200:center[0]+200, center[1]-200:center[1]+200])
center_crop(image)